package com.hrt.manage.menus.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.ViewResource;
import com.hrt.manage.menus.dao.ViewResourceDao;

@Repository
public class ViewResourceDaoImpl extends BaseDaoImpl<ViewResource> implements
		ViewResourceDao {

	@Override
	public List<ViewResource> queryByPage(Integer scope, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		List<ViewResource> results = new ArrayList<ViewResource>();

		final String hql = "select r from ViewResource r where r.isdelete=0 and r.scope='"
				+ scope + "'";

		results = this.queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	@Override
	public Long totalCount(Integer scope) {

		Long total = 0l;
		final String hql = "select count(r) from ViewResource r where r.isdelete=0 and r.scope='"
				+ scope + "'";

		total = this.getHibernateTemplate().execute(
				new HibernateCallback<Long>() {

					@Override
					public Long doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery(hql);
						return (Long) q.uniqueResult();

					}

				});
		return total;
	}

	@Override
	public ViewResource getViewResourceById(Long id) {

		final String hql = "select r from ViewResource r where r.isdelete=0 and r.id='"
				+ id + "'";
		ViewResource viewResourc = this.getHibernateTemplate().execute(
				new HibernateCallback<ViewResource>() {

					@Override
					public ViewResource doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery(hql);
						return (ViewResource) q.uniqueResult();
					}
				});
		return viewResourc;
	}

	@Override
	public List<ViewResource> queryViewResByRole(Long roleId) {
		// TODO Auto-generated method stub
		List<ViewResource> resources = new ArrayList<ViewResource>();
		String hql = "select res from ViewResource res where res.isdelete=0 and res.id  in(select acc.resId from RoleAccess acc where acc.isdelete=0 and acc.roleId="
				+ roleId + ") and res.isdelete=0";
	
		resources = this.getHibernateTemplate().find(hql);
		return resources;
	}
	
	@Override
	public List<ViewResource> queryViewResByRole(Long roleId,Integer scope) {
		// TODO Auto-generated method stub
		List<ViewResource> resources = new ArrayList<ViewResource>();
		String hql = "select res from ViewResource res where res.isdelete=0 and res.id not in(select acc.resId from RoleAccess acc where acc.isdelete=0 and acc.roleId="
				+ roleId + ") and res.isdelete=0 and res.scope="+scope+"";
	
		resources = this.getHibernateTemplate().find(hql);
		return resources;
	}

	@Override
	public List<ViewResource> listManageMenuns(Integer scope) {
		List<ViewResource> results = new ArrayList<ViewResource>();
		System.out.println("------------------>scope"+scope);
		final String hql = "select r from ViewResource r where r.isdelete=0 and r.scope="
				+ scope + "";
		results=this.getHibernateTemplate().find(hql);
		return results;
	}

	@Override
	public List<ViewResource> listManageMenuns(Long roleId, Integer scope) {
		
		List<ViewResource> resources = new ArrayList<ViewResource>();
		
		String hql = "select res from ViewResource res where res.isdelete=0 and res.id not in(select acc.resId from RoleAccess acc where acc.isdelete=0 and acc.roleId="
				+ roleId + ") and res.isdelete=0 and res.scope="+scope+"";
	
		resources = this.getHibernateTemplate().find(hql);
		return resources;
	}

}
